import TableData from "@/components/public/el-table"; //主页面列表

import TableLyout from '@/components/commonPublic/tableLyout.vue'
import actions from "@/actions";
import messageBox from "@/components/public/messageBox"; //主页面分页

import {
    getPurveyorDataList, //列表页接口
    deletePurveyorDataById ,

} from "@/api/request.js";
import publicForm from "@/components/materialDataManage/fromCom.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置

export default {
    name: "supplier",
    data() {
        return {
            name: "supplier",
            expandRows: [], // 展开数组
            userNature: null, //用户Nature
            showMsgBox: false,
            msgTip: "请先选中数据行！",
            msgType: 1,
            isLock: false,
            projectId: null,
            projectName: null,
            navigatepageNums: null,
            loading: false,
            btnObj: formJs.btnObj, //按钮
            queryCriteriaData: {}, //分页
            searchObj: formJs.searchObj, //搜索条件
            table_config: formJs.table_config, //列表
            radio: "待办",
            // 列表
            getList: {
                page: 1,
                pageSize: 15,
                merchantName: "", //生产厂家
                typ: "2" //"1"材料生产商，"2"材料供应商
            },
            isShow: false, //是否显示弹框

            statusNum: null, //点击那个按钮
            dataFid: null, //列表详情id
            currentRow: null, //列表详情
            activeIndex: "1",
            fId: null, //当前数据fId
            tableData: [],
            companyId: null,
            currentRowId: "",
            dialogTitle: null,
        };
    },
    components: {
        TableLyout,
        publicForm,
        TableData,
        messageBox
    },
    computed: {
        hIndex() {
            // 当前页数 - 1 * 每页数据条数 + 1
            return (this.getList.page - 1) * this.getList.pageSize + 1
        }
    },
    mounted() {

        actions.onGlobalStateChange((state, prev) => {
            var data = prev.currentUser.projects;
            this.userInfo = prev.currentUser
        }, true);
        this.yj_searchForm(this.getList);
       
    },
    methods: {
        confirmMsg() {
            this.showMsgBox = false;
        },
        confirmText(status) {
            if (status == '删除') {
                this.msgTip = "此操作将永久删除该数据, 是否继续?";
                this.titelName = "删除";
            } 
            this.msgType = 2; // 0 带功能确定  1 无功能确定  2 带功能确定、取消
            this.showMsgBox = true;

        },
        // 带功能确定
        confirmMsgs() {
            this.showMsgBox = false;
            if (this.titelName == "删除") {
                this.yj_delete();
            } 
        },
        refreshList(status) { //刷新列表

            if(status==0){
                this.isShow = true;
            }
            this.$nextTick(() => {
                this.yj_searchForm(this.getList);
            })
        },
        closeDialog() {
            this.isShow = false;
        },
        handleRow() {},
      
        getBtnName(name, status) {
            this.statusNum = status
            this.yj_edit(status)
        },

       
        editText() {
            this.isShow = true;
        },
        yj_edit(num) {
            this.statusNum = num;
            var data ;
            if (num == 1) { //新增
      
                this.$router.push({
                    path: "/rawMaterialManage/filingMaterialsManage/supplierText",
                });
               data = {
                    data: this.currentRow,
                    getList: this.getList,
                    status: num,
                    typ:2
                }
            } else if (num == 2) { //  编辑
                if (this.dataFid != null) {
                    this.$router.push({
                        path: "/rawMaterialManage/filingMaterialsManage/supplierText",
                    });
                   data = {
                        data: this.currentRow,
                        getList: this.getList,
                        status: num,
                        typ:2
                    }
                } else {
                    this.$message.warning({
                        content: '请先选择需要编辑的数据',
                        duration: 1000
                    })
                }
            } else if (num == 3) { //删除
                if (this.dataFid != null) {
                    this.yj_delete()
                } else {
                    this.$message.warning({
                        content: '请先选择需要删除的数据',
                        duration: 1000
                    })
                }
            }
            else if (num == 7) { //查看
                if (this.dataFid != null) {
                    this.statusNum = 7
                    this.$router.push({
                        path: "/rawMaterialManage/filingMaterialsManage/supplierText",
                    });
                   data = {
                        data: this.currentRow,
                        getList: this.getList,
                        status: num,
                        typ:2
                    }
                } else {
                    this.$message.warning({
                        content: '请先选择需要查看的数据',
                        duration: 1000
                    })
                }
            }
            sessionStorage.setItem(
                "GxEmbedded_primaryInfo",
                JSON.stringify(data)
            );
            
    },
     
        // 查看
        // 点击查看
        yj_viewForm(row, label) {
            if (label == "材料供应商") {
                this.statusNum = 7
                this.currentRow = row;
                this.$router.push({
                    path: "/rawMaterialManage/filingMaterialsManage/supplierText",
                });
              var data = {
                    data: this.currentRow,
                    getList: this.getList,
                    status: this.statusNum,
                    typ:1
                }

                sessionStorage.setItem(
                    "GxEmbedded_primaryInfo",
                    JSON.stringify(data)
                );

            }
        },
        // 点击当前行变色
        rowChange(val, expandedRows) {
            this.currentRow = val
            this.dataFid = val.fId;
            if (expandedRows.length > 1) {
                this.$refs.chartTable.toggleRowExpansion(expandedRows[0])
            }
        },
   // 获取列表数据
   yj_searchForm(data) {
            
    // var obj = {
    //     page: 1,
    //     pageSize: 20,
    //     merchantName: "", //生产厂家
    //     typ: "1" //"1"材料生产商，"2"材料供应商
    // }
    this.loading = true;
    var that = this;
    getPurveyorDataList(data)
        .then((res) => {
            if (res.errcode == 0) {
                that.loading = false;
                that.tableData = res.data.pageInfo.list;
                this.queryCriteriaData = res.data.pageInfo;
            } else {
                this.$message.error({
                    content: '列表数据获取失败' + res.msg,
                    duration: 1000
                })
                this.loading = false;
            }
        })
        .catch((error) => {
            this.$message.error({
                content: '列表数据获取失败' + error.msg,
                duration: 1000
            })
            this.loading = false;
        });
},
           // 删除
           yj_delete() {
            if (this.dataFid != "" && this.dataFid != null) {
               
                        this.loading = true;
                        this.isLock = true
                        deletePurveyorDataById({
                                fId: this.dataFid,
                                // userId: this.userInfo.userId, //当前登陆用户ID
                                // userName: this.userInfo.realName, //当前登陆用户名称
                            })
                            .then((res) => {
                                if (res.errcode == 0) {
                                    this.isLock = false
                                    this.loading = false;
                                    this.$message.success({
                                        content: "删除成功",
                                        duration: 1000
                                    })
                                    this.yj_searchForm(this.getList);
                                    this.dataFid = null

                                } else {
                                    this.isLock = false
                                    this.$message.error({
                                        content: "删除失败"+res.msg,
                                        duration: 1000
                                    })
                                    this.loading = false;
                                }
                            })
                            .catch(() => {
                                console.log("删除失败");
                                this.loading = false;
                            });
                 
            } else {
                this.$message.warning({
                    content: "请先选择需要删除的数据",
                    duration: 1000
                })
            }
        },
    },
};